/**
 * 
 */
package com.EstateManager.Dao;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.EstateManager.Entity.Admin;
import com.EstateManager.Entity.User;

/**
 * 创建时间 : 下午9:58:23
 * 
 * @author Yonggang Liu
 *
 *         AdminDao.java
 */
@Repository("adminDao")
public class AdminDao implements IModelBasicOperate<Admin, Integer> {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public Admin load(Integer id) {

		return (Admin) this.getCurrentSession().load(Admin.class, id);
	}

	@Override
	public Admin get(Integer id) {

		return (Admin) this.getCurrentSession().get(Admin.class, id);
	}

	@Override
	public void persist(Admin entity) {

		this.getCurrentSession().persist(entity);
	}

	@Override
	public Integer save(Admin entity) {

		return (Integer) this.getCurrentSession().save(entity);
	}

	@Override
	public void saveOrUpdate(Admin entity) {
		this.getCurrentSession().saveOrUpdate(entity);
	}

	/* (non-Javadoc)
	 * @see com.EstateManager.Dao.IModelBasicOperate#detete(java.lang.Object)
	 */
	@Override
	public void detete(Admin entity) throws HibernateException {
		this.getCurrentSession().delete(entity);
		
	}
	@Override
	public void flush() {
		this.getCurrentSession().flush();
	}

	public Admin getAdminByUsername(String userName) {

	
		Session session = this.getCurrentSession();
		Query query = session.createQuery("select admin " 
						+ "from Admin admin " 
						+ "where admin.username = :username");
		query.setString("username", userName);
		return (Admin)query.uniqueResult();
	}

	@Override
	public void update(Admin t) throws HibernateException {
		this.getCurrentSession().update(t);
	}

	@Override
	public void merge(Admin t) throws HibernateException {
		this.getCurrentSession().merge(t);
	}

	/**
	 * 根据昵称查询管理员
	 * @param nickName 用户昵称
	 * @return user
	 * */	
	public Admin getAdminByNickName(String nickName) {
	Session session = this.getCurrentSession();
		Query query = session.createQuery("select admin"
				+ " from Admin admin"
				+ " where admin.nickName = :nickName");
		query.setString("nickName", nickName);
		return (Admin)query.uniqueResult();
}
	
	
	
}
